/**
 * Copyright (c) 2011-2015 1lifes.com
 * HSJ java-handlersocket
 * Id: FindPacket.java
 */
package com.elifes.hsj.packet;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.elifes.hsj.model.CompareOperator;
import com.elifes.hsj.model.Filter;
import com.elifes.hsj.model.INFilter;
import com.elifes.hsj.model.Modify;

/**
 * 描述：
 * @author yangqiang
 * @createtime 2011-11-12下午5:10:53
 *
 */
public class ModifyPacket extends AbstractPacket{
	private FindPacket findPacket;
	private Modify mod;
	
	public ModifyPacket(FindPacket findPacket, Modify mod){
		this.type = PacketEnum.MOD;
		this.indexId = findPacket.getIndexId();
		this.findPacket = findPacket;
		this.mod = mod;
	}

	@Override
	protected void encodeHeader(StringBuilder reqMsg) {}

	@Override
	protected void encodeBody(StringBuilder reqMsg) {
		findPacket.encodeBody(reqMsg);
		
		reqMsg.append(autoAppendTab(mod.getType().getValue()));
		if(mod.getModValues() != null){
			for(String modValue : mod.getModValues()){
				reqMsg.append(autoAppendTab(modValue));
			}
		}
	}

	@Override
	public Object decode() {
		if(mod.getType().getValue().contains("?")){
			return findPacket.decode();
		} else {
			//result is: 0 1 <nummod>
			return ((String)getResult()).substring(4);
		}
	}

}
